﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace PRPSChart
{
    public class PRPSChart : UserControl

    {
        public PRPSChart()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            this.BackColor = Color.Gray;
    
        }

        public float[, ] Data = new float[50, 72];

        public int MaxCycle = 50;

        protected Grid Grid = new Grid();

        protected Palette Palette = new Palette();
     
        public void Set(float[,] data)
        {
            Data = data;
            this.Invalidate();
        }

        protected  void DrawBar(Graphics g)
        {

            for (int i = 0; i < Data.GetLength(0); i++)
            {
                for (int j = 0; j < Data.GetLength(1); j++)
                {
                    Grid.DrawBar(g, (float)j, (float)i, (float)Data[i, j], Palette.GetColor(Data[i, j]));
                }
            }
        }

      
        protected void DrawAll(Graphics g)
        {

            float w = (float)this.ClientRectangle.Width;
            float h = (float)this.ClientRectangle.Height;

            if (w / h < 1.3)
            {
                h = w / 1.3f;
            }
            else
            {
                w = h * 1.3f;
            }

            Grid.Init(new PointF(w * 0.2f, h * 0.5f),                     
                      new XYZ(w * 0.3f, h * 0.6f, h * 0.25f));


            Grid.RotateAngle = 60 / 180.0 * Math.PI;
            Grid.Font = this.Font;
            Grid.DrawGrid(g);
            Grid.DrawString(g);
            Palette.Location = new PointF(w * 0.02f, h * 0.05f);
            Palette.Size = new SizeF(w * 0.03f, h * 0.4f);

            Palette.Font = this.Font;
            Palette.Draw(g);

            DrawBar(g);
        }



        protected override void OnPaint(PaintEventArgs e)
        {

            Graphics g = e.Graphics;

            DrawAll(g);

            base.OnPaint(e);

        }

   
        protected override void OnSizeChanged(EventArgs e)
        {
            this.Font = new Font(this.Font.FontFamily, this.Height / 30);
            this.Invalidate();   
            base.OnSizeChanged(e);
        }
    }



       
    
}
